package com.yonyou.pmclouds.project.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.service.CommonPropertiesProcessor;
import com.yonyou.pmclouds.organization.entity.OrganizationVO;
import com.yonyou.pmclouds.organization.rmiitf.OrganizationQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.plan.rmiitf.ProjectPlanQuery;
import com.yonyou.pmclouds.problemlog.entity.ProblemLogVO;
import com.yonyou.pmclouds.problemlog.service.rmiitf.ProblemLogQuery;
import com.yonyou.pmclouds.project.entity.ProjectBillStatisticsVO;
import com.yonyou.pmclouds.project.entity.ProjectRefVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.projecttype.entity.ProjectTypeVO;
import com.yonyou.pmclouds.projecttype.rmiitf.ProjectTypeQuery;
import com.yonyou.pmclouds.team.rmiitf.TeamMemberQuery;
import com.yonyou.pmclouds.user.entity.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/mobile/project")
public class ProjectMobController {

    private static final String[] USER_NAME_FIELDS = {ProblemLogVO.RECTIFICATIONER_NAME, ProblemLogVO.CREATOR_NAME};

    @Autowired
    private UserInfoQuery userQuery;

    @Autowired
    private CommonPropertiesProcessor propertiesProcessor;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProjectSimpleQuery query;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProjectPlanQuery projectPlanQuery;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProblemLogQuery problemLogQuery;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private TeamMemberQuery teamMemberQuery;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProjectTypeQuery typeQuery;

    private static final String[] USER_ID_FIELDS = {ProblemLogVO.PK_RECTIFICATIONER, ProblemLogVO.CREATOR};

    @Value("${pmcloud.useNewProjectTeamTenant}")
    private String useNewTeamTenantIds;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private OrganizationQuery organizationQuery;

    /**
     * 是否使用新版本的项目团队，false继续使用原有接口
     */
    @RequestMapping("/isUseNewTeam")
    public boolean isUseNewTeam() {
        boolean flag = false;
        String tenantId = RuntimeEnvironment.getTenantId();
        if (StringUtils.isNotEmpty(useNewTeamTenantIds) && useNewTeamTenantIds.contains(tenantId)) {
            flag = true;
        }

        return flag;
    }

    @RequestMapping("/statistic")
    public ProjectBillStatisticsVO[] getProjectStatistic(@RequestParam("projectId") String projectId) throws BusinessException {
        return query.queryStatistics(projectId);
    }

    @RequestMapping("/projectInfo")
    public ProjectRefVO queryProject(@RequestParam("projectId") String projectId, @RequestParam("tenantId") String tenantId) throws BusinessException {
        Map<String, ProjectRefVO> projectRefVOMap = query.queryRefs(new String[]{projectId});
        if (projectRefVOMap == null || projectRefVOMap.isEmpty()) {
            return null;
        }
        // 补充组织信息
        ProjectRefVO next = projectRefVOMap.values().iterator().next();
        if (next != null) {
            if (next.getPkOrg() != null) {
                OrganizationVO organizationVO = organizationQuery.queryByPkSrc(next.getPkTenant(), next.getPkOrg());
                if (organizationVO != null) {
                    next.setOrgCode(organizationVO.getScode());
                    next.setOrgName(organizationVO.getSname());
                }
            }
            // 补充项目经理
            if (next.getManager() != null) {
                UserInfo userInfo = userQuery.queryUser(next.getManager());
                if (userInfo != null) {
                    next.setManagerName(userInfo.getUserName());
                }
            }
            if (next.getPkProjectType() != null) {
                ProjectTypeVO projectTypeVO = typeQuery.queryById(next.getPkProjectType());
                if (projectTypeVO != null) {
                    next.setProjectTypeName(projectTypeVO.getSname());
                }
            }
        }
        return next;
    }

    @RequestMapping("/getTotalProgress")
    public Integer getTotalProgress(@RequestParam("projectId") String projectId) throws BusinessException {
        // 统计项目总进度
        Map<String, Integer> projectProgressMap = projectPlanQuery.getTotalProgress(new String[]{projectId});
        if(projectProgressMap == null || projectProgressMap.isEmpty()) return 0;
        return projectProgressMap.get(projectId);
    }

    /**
     * 计数项目下的成员数量
     *
     * @param projectid
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/countProjectMembers")
    public Integer countProjectMembers(@RequestParam("projectid") String projectid) throws BusinessException {
        return teamMemberQuery.countProjectMembers(projectid);
    }
}
